package 图形界面UI;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

public class FileCopy extends JFrame implements ActionListener {
    JButton button1;
    JButton button2;
    JButton copyBtn;
    JTextField textField1;
    JTextField textField2;
    JProgressBar progressBar;

    public FileCopy() {
        setTitle("文件复制");
        setSize(800,500);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);
        setLayout(null);

        JLabel label1 = new JLabel("源文件");
        textField1 = new JTextField();
        button1 = new JButton("...");
        label1.setBounds(100,50,60,25);
        add(label1);
        textField1.setBounds(170,50,400,25);
        add(textField1);
        button1.setBounds(680,50,50,25);
        add(button1);
        button1.addActionListener(this);

        JLabel label2 = new JLabel("目的地");
        textField2 = new JTextField();
        button2 = new JButton("...");
        label2.setBounds(100,100,60,25);
        add(label2);
        textField2.setBounds(170,100,400,25);
        add(textField2);
        button2.setBounds(680,100,50,25);
        add(button2);
        button2.addActionListener(this);

        copyBtn = new JButton("复制");
        copyBtn.setBounds(250,200,200,25);
        add(copyBtn);
        copyBtn.addActionListener(this);

        progressBar = new JProgressBar();
        progressBar.setBounds(100,300,300,25);
        add(progressBar);
        progressBar.setStringPainted(true);
        progressBar.setValue(0);

    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new FileCopy();
            }
        });
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object o = e.getSource();
        if(o==button1) {
            //System.out.println("1");
            JFileChooser fileChooser = new JFileChooser();
            int f = fileChooser.showOpenDialog(this);

            //System.out.println(fileChooser.getSelectedFile().getPath());
            if(f==JFileChooser.APPROVE_OPTION) {
                textField1.setText(fileChooser.getSelectedFile().getPath());
            }
        }
        else if(o==button2) {
            //System.out.println("2");
            JFileChooser fileChooser = new JFileChooser();
            fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            int f = fileChooser.showOpenDialog(this);
            if (f==JFileChooser.APPROVE_OPTION) {
                textField2.setText(fileChooser.getSelectedFile().getPath());
            }
        }
        //复制
        else if (o==copyBtn){
            String srcPath = textField1.getText();
            String destPath = textField2.getText();
            File srcFile = new File(srcPath);
            File destFile = new File(destPath+"/"+srcFile.getName());
            new Thread() {
                @Override
                public void run() {
                    copy(srcFile,destFile);
                }
            }.start();
            //copy(srcFile,destFile);
        }

    }

    private void copy(File srcFile, File destFile) {
        try {
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
            byte[] tmp= new byte[1024];
            int len;
            Long totalSize = srcFile.length();
            Long copiedSize=0L;
            while ((len=bis.read(tmp))!=-1){
                bos.write(tmp,0,len);
                copiedSize= copiedSize+len;
                int percent =(int) (1.0*copiedSize/totalSize*100);
                progressBar.setValue(percent);
            }
            bos.close();bis.close();
        }
        catch (FileNotFoundException e){
            e.printStackTrace();
        }
        catch (IOException e) {
            e.printStackTrace();
        }


    }
}
